package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ASUS
 * Date: 2025-04-28
 * Time: 11:23
 */
/*
新帖发布的测试
 */
public class AddPostPage extends Utils {
    public static String url = "http://127.0.0.1:58080/index.html";
    public AddPostPage() {
        super(url);
    }

    // 进入发帖界面
    public void AddPostEnter() throws InterruptedException {
        // 需要重新登陆
        // 返回登录页并刷新页面
        driver.navigate().refresh();
        // 重新登录
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#submit")).click();
        // 点击 "发布帖子" 按钮 进入发帖界面
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")).click();
        // 获取界面元素
        Thread.sleep(1000);
        String ele1 = driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none")).getText();
        // 断言判断是否成功进入
        assert ele1.equals("发新贴");
    }

    // 发布新帖测试
    public void AddPost() throws InterruptedException {
        // 获取标题输入框元素
        WebElement ele2 = driver.findElement(By.cssSelector("#article_post_title"));
        // 点击输入框
        Actions actions = new Actions(driver);
        actions.click(ele2).perform();
        // 输入标题
        actions.sendKeys("自动输入的标题2");
//        // 获取输入的标题内容
//        String ele3 = driver.findElement(By.cssSelector("#article_post_title")).getText();
        // 获取内容输入框元素
        WebElement ele4 = driver.findElement(By.cssSelector("#edit-article > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre"));
        // 点击输入框
        actions.click(ele4).perform();
        // 输入内容
        actions.sendKeys("自动输入的内容2").perform();
        // 点击发布按钮
        // 不能用显式等待, 发布按钮执行太快
        WebElement ele5 = driver.findElement(By.cssSelector("#article_post_submit"));
//        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));
//        wait.until(ExpectedConditions.elementToBeClickable(ele5));
        // 两种点击方式: 1. 界面定位元素点击; 2. 定位元素模拟鼠标点击
//        driver.findElement(By.cssSelector("#article_post_submit")).click();
        Thread.sleep(2000);
        actions.click(ele5).perform();
        // 刷新页面
        driver.navigate().refresh();
        // 获取新页面第一个帖子的标题
        String ele6 = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a")).getText();
        // 断言判断是否发布成功
        assert ele6.equals("自动输入的标题2");
    }
}
